var c = require("../libs/common.js");
var cf = require('../libs/config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 评论
    starl: [1, 1, 1, 1, 1],
    starld: [1, 1, 1, 1, 1],
    hiddeninfo:true
    // 评论
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var did = options.did;

    this.toDidGetInfo(did);
  },
  
  toDidGetInfo(did){
    var uid = wx.getStorageSync('userinfo').id;
    var that = this;
    that.setData({ did: did })
    c.ajax(
      cf.hostapi + 'DoctorDetails/getDoctorInfo',
      { did: did, uid: uid },
      res => {
        console.log(res);
        var dt = res.data.data.info;
        var follow = res.data.data.follow;
        var comment = res.data.data.comment;
        var commentnum = res.data.data.commentnum;
        var starnuml;
        var starnumb;
        for (var i = 0; i < comment.length; i++) {
          comment[i]['starnuml'] = c.stararray(comment[i]['ct_star']);
          comment[i]['starnumb'] = c.stararray(5 - comment[i]['ct_star']);
        }
        that.setData({ dt: dt, follow: follow, comment: comment, commentnum: commentnum });
      }
    )
  },
  
  upinfo(did){
    var uid = wx.getStorageSync('userinfo').id;
    var that = this;
    c.ajax(
      cf.hostapi + 'DoctorDetails/getDoctorInfo',
      { did: did, uid: uid },
      res => {
        console.log(res);
        var dt = res.data.data.info;
        var follow = res.data.data.follow;
        var comment = res.data.data.comment;
        var commentnum = res.data.data.commentnum;
        var starnuml;
        var starnumb;
        for (var i = 0; i < comment.length; i++) {
          comment[i]['starnuml'] = c.stararray(comment[i]['ct_star']);
          comment[i]['starnumb'] = c.stararray(5 - comment[i]['ct_star']);
        }
        that.setData({ dt: dt, follow: follow, comment: comment, commentnum: commentnum });
      }
    )
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  stararray:function($num){
    var array=[];
    for(var i=0;i<$num;i++){
        array[i]=1;
    }
    return array;
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var did=this.data.did;
    this.toDidGetInfo(did);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 收藏
  clickcollect:function(){
    var did=this.data.did;
    var uid = wx.getStorageSync('userinfo').id;
    c.ajax(
      cf.hostapi +'DoctorDetails/addCollect',
      { did:did,uid:uid},
      res=>{
       var status=res.data.data;
        if (status==1){
         wx.showToast({
           title: '收藏成功！',
           image: '/images/dui.png'
         })
       }
        this.setData({ follow: status})
      }
    )
  },
  clickyishoucang:function(){
    var did = this.data.did;
    var uid = wx.getStorageSync('userinfo').id;
    c.ajax(
      cf.hostapi + 'DoctorDetails/delCollect',
      { did: did, uid: uid },
      res => {
        var status = res.data.data;
        if (status == 1) {
          wx.showToast({
            title: '取消收藏成功！',
            image: '/images/dui.png'
          })
        }
        this.setData({ follow: 0 })
      }
    )
  },
  clickzixun:function(){
    var did=this.data.did;
    wx.navigateTo({
      url: '/pages/talkdetile/talkdetile?did='+did,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },



  staradd: function (e) {
    console.log(e)
    var starl = [];
    var starb = [];
    var num = e.currentTarget.dataset.num;

    for (var i = 0; i < num; i++) {
      starl[i] = 1;
    }
    for (var i = 0; i < (5 - num); i++) {
      starb[i] = 1;
    }

    this.setData({
      starl: starl,
      starb: starb
    });
  },
  stardee: function (e) {
    var starb = [];
    var num = e.currentTarget.dataset.num;
    console.log(num);
    var starl = this.data.starl;
    var lcount = starl.length;
    //在亮星的基础上网上添加
    for (var i = lcount; i <= lcount + num; i++) {
      starl[i] = 1;
    }
    var lcountdel = starl.length;
    for (var i = 0; i < (5 - lcountdel); i++) {
      starb[i] = 1;
    }
    this.setData({
      starl: starl,
      starb: starb
    });
  },
  title: function (e) {
    this.data.title = e.detail.value;
  },
  sendpl: function () {
    var that = this;
    var starl = this.data.starl;
    var title = this.data.title;
    var starnum = starl.length;
    var uid = wx.getStorageSync('userinfo').id;
    var goods_id = this.data.goods_id;
    c.ajax(
      app.url + 'sendpl', {
        title: title,
        starnum: starnum,
        user_id: user_id,
        goods_id: goods_id
      },
      res => {

        if (res.data == 1) {
          this.setData({
            hiddeninfo: true,
          })
          that.onLoad(goods_id);
        }
        wx.showToast({
          title: '发表成功',
        })


      }
    )
  },
  deletes: function () {
    this.setData({
      hiddeninfo: true,
    })
  }


})